package netiq

import (
	"bytes"
	"net/url"
	"os"
	"testing"

	"github.com/PuerkitoBio/goquery"
	"github.com/stretchr/testify/require"
	"github.com/versent/saml2aws/v2/pkg/page"
)

func TestIsSAMLResponsePositive(t *testing.T) {
	//given
	samlResponseData, err := os.ReadFile("responses/samlRespose.html")
	require.Nil(t, err)

	//when
	samlRespDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(samlResponseData))
	require.Nil(t, err)

	//then
	require.True(t, isSAMLResponse(samlRespDoc))
}

func TestIsSAMLResponseNegative(t *testing.T) {
	//given
	getToContentData, err := os.ReadFile("responses/getToContent.html")
	require.Nil(t, err)

	//when
	getToContentDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(getToContentData))
	require.Nil(t, err)

	//then
	require.False(t, isSAMLResponse(getToContentDoc))
}

func TestExtractSAMLAssertion(t *testing.T) {
	//given
	samlResponseData, err := os.ReadFile("responses/samlRespose.html")
	require.Nil(t, err)
	expectedResult := "PHNhbWxwOlJlc3BvbnNlIHhtbG5zOnNhbWxwPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wiIHhtbG5zOnNhbWw9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphc3NlcnRpb24iIERlc3RpbmF0aW9uPSJodHRwczovL3NpZ25pbi5hd3MuYW1hem9uLmNvbS9zYW1sIiBJRD0iaWRteFVmZUR5dVJhODlaTWtEOUg3Z3pKQl9tUTQiIElzc3VlSW5zdGFudD0iMjAyMC0wMy0wNVQwMjowNToxOFoiIFZlcnNpb249IjIuMCI+PHNhbWw6SXNzdWVyPmh0dHBzOi8vbG9naW4uYXV0aGJyaWRnZS53ZXN0cGFjZ3JvdXAuY29tL25pZHAvc2FtbDIvbWV0YWRhdGE8L3NhbWw6SXNzdWVyPjxzYW1scDpTdGF0dXM+PHNhbWxwOlN0YXR1c0NvZGUgVmFsdWU9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDpzdGF0dXM6U3V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jg2OnNhbWwtcHJvdmlkZXIvd2VzdHBhY2lkcDwvc2FtbDpBdHRyaWJ1dGVWYWx1ZT48c2FtbDpBdHRyaWJ1dGVWYWx1ZSB4c2k6dHlwZT0ieHM6c3RyaW5nIj5hcm46YXdzOmlhbTo6MTY5MjY3NDUxMjg1OnJvbGUvd2JjLXJlYWRvbmx5LGFybjphd3M6aWFtOjoxNjkyNjc0NTEyODU6c2FtbC1wcm92aWRlci93ZXN0cGFjaWRwPC9zYW1sOkF0dHJpYnV0ZVZhbHVlPjwvc2FtbDpBdHRyaWJ1dGU+PC9zYW1sOkF0dHJpYnV0ZVN0YXRlbWVudD48L3NhbWw6QXNzZXJ0aW9uPjwvc2FtbHA6UmVzcG9uc2U+"

	//when
	samlResponseDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(samlResponseData))
	require.Nil(t, err)
	actualResult, err := extractSAMLAssertion(samlResponseDoc)
	require.Nil(t, err)

	//then
	require.Equal(t, expectedResult, actualResult)
}

func TestExtractGetToContentUrlPositive(t *testing.T) {
	//given
	getToContentData, err := os.ReadFile("responses/getToContent.html")
	require.Nil(t, err)
	expectedResourceUrl := "/nidp/jsp/content.jsp?sid=0&option=credential&id=AWS"

	//when
	getToContentDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(getToContentData))
	require.Nil(t, err)
	actualResourceUrl, ok := extractGetToContentUrl(getToContentDoc)

	//then
	require.True(t, ok)
	require.Equal(t, expectedResourceUrl, actualResourceUrl)
}

func TestExtractGetToContentUrlNegative(t *testing.T) {
	//given
	samlResposeData, err := os.ReadFile("responses/samlRespose.html")
	require.Nil(t, err)

	//when
	samlResposeDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(samlResposeData))
	require.Nil(t, err)
	actualResourceUrl, ok := extractGetToContentUrl(samlResposeDoc)

	//then
	require.False(t, ok)
	require.Equal(t, "", actualResourceUrl)
}

func TestExtractGetToContentUrlDiv(t *testing.T) {
	//given
	getToContentData, err := os.ReadFile("responses/getToContentDiv.html")
	require.Nil(t, err)
	expectedResourceUrl := "/nidp/app/login?id=contract_kerb&sid=0&option=credential&sid=0"

	//when
	getToContentDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(getToContentData))
	require.Nil(t, err)
	actualResourceUrl, ok := extractGetToContentUrl(getToContentDoc)

	//then
	require.True(t, ok)
	require.Equal(t, expectedResourceUrl, actualResourceUrl)
}

func TestExtractWinLocHrefUrlPositive(t *testing.T) {
	//given
	winLocHrefData, err := os.ReadFile("responses/winLocHref.html")
	require.Nil(t, err)
	expectedResourceUrl := "https://login.authbridge.somegroup.com/nidp/saml2/idpsend?PID=STSPv8a5kc"

	//when
	winLocHrefDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(winLocHrefData))
	require.Nil(t, err)
	actualResourceUrl, ok := extractWinLocHrefURL(winLocHrefDoc)

	//then
	require.True(t, ok)
	require.Equal(t, expectedResourceUrl, actualResourceUrl)
}

func TestExtractWinLocHrefUrlNegative(t *testing.T) {
	//given
	samlResposeData, err := os.ReadFile("responses/samlRespose.html")
	require.Nil(t, err)

	//when
	samlResposeDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(samlResposeData))
	require.Nil(t, err)
	actualResourceUrl, ok := extractWinLocHrefURL(samlResposeDoc)

	//then
	require.False(t, ok)
	require.Equal(t, "", actualResourceUrl)
}

func TestExtractIDPLoginPassPositive(t *testing.T) {
	//given
	idpLoginPassData, err := os.ReadFile("responses/idpLoginPass.html")
	require.Nil(t, err)
	expectedForm := &page.Form{
		URL:    "https://login.authbridge.somegroup.com/nidp/app/login?sid=0&sid=0",
		Method: "POST",
		Values: &url.Values{},
	}

	//when
	idpLoginPassDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(idpLoginPassData))
	require.Nil(t, err)
	actualForm, ok := extractIDPLoginPass(idpLoginPassDoc)

	//then
	require.True(t, ok)
	require.Equal(t, expectedForm, actualForm)
}

func TestExtractIDPLoginPassNegative(t *testing.T) {
	//given
	idpLoginRsaData, err := os.ReadFile("responses/idpLoginRsa.html")
	require.Nil(t, err)

	//when
	idpLoginRsaDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(idpLoginRsaData))
	require.Nil(t, err)
	actualResourceUrl, ok := extractIDPLoginPass(idpLoginRsaDoc)

	//then
	require.False(t, ok)
	require.Nil(t, actualResourceUrl)
}

func TestExtractPrivilegedIDPLoginPassPositive(t *testing.T) {
	//given
	idpLoginPassData, err := os.ReadFile("responses/privileged_flow/idpLoginPass.html")
	require.Nil(t, err)
	expectedForm := &page.Form{
		URL:    "https://login.authbridge.somegroup.com/nidp/app/login?sid=0&sid=0",
		Method: "POST",
		Values: &url.Values{},
	}

	//when
	idpLoginPassDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(idpLoginPassData))
	require.Nil(t, err)
	actualForm, ok := extractIDPLoginPass(idpLoginPassDoc)

	//then
	require.True(t, ok)
	require.Equal(t, expectedForm, actualForm)
}

func TestExtractIDPLoginRsaPositive(t *testing.T) {
	//given
	idpLoginRsaData, err := os.ReadFile("responses/idpLoginRsa.html")
	require.Nil(t, err)
	expectedForm := &page.Form{
		URL:    "https://login.authbridge.somegroup.com/nidp/app/login?sid=11&sid=11",
		Method: "POST",
		Values: &url.Values{},
	}

	//when
	idpLoginRsaDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(idpLoginRsaData))
	require.Nil(t, err)
	actualForm, ok := extractIDPLoginRsa(idpLoginRsaDoc)

	//then
	require.True(t, ok)
	require.Equal(t, expectedForm, actualForm)
}

func TestExtractIDPLoginRsaNegative(t *testing.T) {
	//given
	idpLoginPassData, err := os.ReadFile("responses/idpLoginPass.html")
	require.Nil(t, err)

	//when
	idpLoginPassDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(idpLoginPassData))
	require.Nil(t, err)
	actualResourceUrl, ok := extractIDPLoginRsa(idpLoginPassDoc)

	//then
	require.False(t, ok)
	require.Nil(t, actualResourceUrl)
}

func TestExtractPrivilegedIDPLoginRsaNegative(t *testing.T) {
	//given
	idpLoginPassData, err := os.ReadFile("responses/privileged_flow/idpLoginPass.html")
	require.Nil(t, err)

	//when
	idpLoginPassDoc, err := goquery.NewDocumentFromReader(bytes.NewReader(idpLoginPassData))
	require.Nil(t, err)
	actualResourceUrl, ok := extractIDPLoginRsa(idpLoginPassDoc)

	//then
	require.False(t, ok)
	require.Nil(t, actualResourceUrl)
}

func TestPrivilegedLoginUrl(t *testing.T) {
	//given
	mfa := "Privileged"
	baseUrl := "https://abc.com"
	defaultResourcePath := "/login.html"
	expectedLoginUrl := baseUrl + "/nidp/app/login?id=privacc&sid=0&option=credential"

	//when
	loginUrl, err := getLoginUrl(mfa, baseUrl, defaultResourcePath)

	//then
	require.Nil(t, err)
	require.Equal(t, loginUrl, expectedLoginUrl)
}

func TestDefaultLoginUrl(t *testing.T) {
	//given
	mfa := "Auto"
	baseUrl := "https://abc.com"
	defaultResourcePath := "/login.html"
	expectedLoginUrl := baseUrl + defaultResourcePath

	//when
	loginUrl, err := getLoginUrl(mfa, baseUrl, defaultResourcePath)

	//then
	require.Nil(t, err)
	require.Equal(t, loginUrl, expectedLoginUrl)
}

func TestUnsupportedMFA(t *testing.T) {
	//given
	mfa := "None"
	baseUrl := "https://abc.com"
	defaultResourcePath := "/login.html"
	expectedErrorString := "Unsupported MFA"

	//when
	_, err := getLoginUrl(mfa, baseUrl, defaultResourcePath)

	//then
	require.EqualError(t, err, expectedErrorString)
}
